﻿namespace TestsRT
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    using KeePassLib;
    using KeePassLib.Interfaces;
    using KeePassLib.Keys;
    using KeePassLib.Serialization;

    using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

    using Windows.ApplicationModel;
    using Windows.Foundation;
    using Windows.Security.Cryptography.Core;
    using Windows.Storage;
    using Windows.UI.Core;
    using Windows.UI.Xaml;

    [TestClass]
    public class Basics
    {
        [TestMethod]
        public async Task TestMethod1()
        {
            string dbPath = (await GetFile("Test.kdbx")).Path;

            PwDatabase db = new PwDatabase();
            IOConnectionInfo connectionInfo = IOConnectionInfo.FromPath(dbPath);

            CompositeKey key = new CompositeKey();
            key.AddUserKey(new KcpPassword("ThisIsMyPassword!"));
            // string keyFilePath = (await GetFile("KeyFile.txt")).Path;
            // key.AddUserKey(new KcpKeyFile(keyFilePath));
            db.Open(connectionInfo, key, new NullStatusLogger());

            string tempPath = ApplicationData.Current.TemporaryFolder.Path;
            string newFile = Path.Combine(tempPath, "Test.kdbx");
            IOConnectionInfo newLocation = IOConnectionInfo.FromPath(newFile);

            db.SaveAs(newLocation, false, new NullStatusLogger());
        }

        [TestMethod]
        public void HashTest()
        {
            var sha256 = new SHA256Managed();

            var expectedHash = new byte[]
                                   {
                                       161, 40, 113, 254, 226, 16, 251, 134, 25, 41, 30, 174, 161, 148, 88, 28, 189, 37,
                                       49, 228, 178, 55, 89, 210, 37, 246, 128, 105, 35, 246, 50, 34,
                                   };

            byte[] hash = sha256.ComputeHash(new byte[] { 1, 2, 3, 4 }, 0, 2);

            CollectionAssert.AreEqual(expectedHash, hash);
        }

        private static async Task<StorageFile> GetFile(string name)
        {
            StorageFolder loc = Package.Current.InstalledLocation;
            StorageFolder files = await loc.GetFolderAsync("Files");
            return await files.GetFileAsync(name);
        }
    }
}
